//Copyright (C) 2009 Ron Coleman. Contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

/*
 * WorkRequiredDialog.java
 *
 * Created on Oct 18, 2009, 7:13:26 PM
 */

package org.workplicity.marist.grounds.ui;

import org.workplicity.marist.grounds.GroundsRequest;
import org.workplicity.marist.grounds.util.GroundsDate;
import org.workplicity.marist.grounds.worklet.GroundsWorklet;
import java.util.ArrayList;
import javax.swing.JDialog;
import org.workplicity.entry.User;
import org.workplicity.entry.WorkRequest;
import org.workplicity.entry.WorkSlate;
import org.workplicity.marist.grounds.util.GroundsHelper;
import org.workplicity.util.Helper;
import org.workplicity.util.Resources;
import org.workplicity.util.WorkDate;
import org.workplicity.worklet.WorkletContext;

/**
 *
 * @author Ron Coleman
 */
public class WorkRequiredDialog extends javax.swing.JDialog {
    private GroundsRequest order;

    private WorkSlate assigned = null;

    private WorkletContext context = GroundsWorklet.getInstance().getContext();

    /** Creates new form WorkRequiredDialog */
    public WorkRequiredDialog(JDialog parent,GroundsRequest order,WorkSlate assigned) {
        super(parent, true);
        initComponents();

        this.setTitle("Grounds - Work required");
        this.setIconImage(Resources.logoSmall.getImage());
        this.setLocationRelativeTo(parent);

        this.order = order;
        
        this.assigned = assigned;

        initWithOrder();
    }

    private void initWithOrder() {
        // Get the work slates only for the current user and put them
        // on the combo box

        ArrayList<WorkSlate> workslates = null;

        // Put all work slates on the combo box, all if power user
        // or just mine otherwise
        if(Helper.isSuperUser(context)) {
            workslates = GroundsWorklet.getInstance().getWorkSlatesCache().getCache();
        }
        else {
            User me = (User) context.read(Resources.TAG_USER);

            workslates = GroundsHelper.getWorkSlates(me.getWorkSlateList());
        }

        for (WorkSlate workslate : workslates) {
            this.workslatesComboBox.addItem(workslate);
        }


        // If the order is already assigned to a work slate
        // set the work slate combo box to this work slate
        // and disable the combo box since the combo box
        // can't be changed (here) if it is set.
        if(assigned != null) {
            this.workslatesComboBox.setSelectedItem(assigned);

            // Can't change work slate here if it is already set (sorry)
//            this.workslatesComboBox.setEnabled(false);

            GroundsDate date = new GroundsDate(order.getAssignedDate());
            this.assignedDateTextField.setText(date.toString());
        }
        else {
            this.workslatesComboBox.setSelectedIndex(0);
            this.assignedDateTextField.setText("N/A");
        }

        // Set the rest of the fields
        this.workRequiredTextArea.setText(order.getDescr());
        this.chargeToTextField.setText(order.getChargeTo());

        if(order.getPriority() == GroundsRequest.Priority.URGENT)
            this.urgentButton.setSelected(true);
        else if(order.getPriority() == GroundsRequest.Priority.ROUTINE)
            this.routineButton.setSelected(true);

        this.reasonTextArea.setText(order.getReason());

        if(order.getStatus() == WorkRequest.Status.CLOSED)
            this.saveButton.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        priorityButtonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        workRequiredTextArea = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        chargeToTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        urgentButton = new javax.swing.JRadioButton();
        routineButton = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        reasonTextArea = new javax.swing.JTextArea();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        assignedDateTextField = new javax.swing.JTextField();
        workslatesComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Work required:");

        jScrollPane1.setFont(new java.awt.Font("Tahoma", 0, 14));

        workRequiredTextArea.setColumns(20);
        workRequiredTextArea.setFont(new java.awt.Font("Monospaced", 0, 12));
        workRequiredTextArea.setRows(5);
        jScrollPane1.setViewportView(workRequiredTextArea);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Charge to:");

        chargeToTextField.setFont(new java.awt.Font("Tahoma", 0, 12));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Priority:");

        priorityButtonGroup.add(urgentButton);
        urgentButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        urgentButton.setText("Urgent");

        priorityButtonGroup.add(routineButton);
        routineButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        routineButton.setSelected(true);
        routineButton.setText("Routine");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText("Reason:");

        reasonTextArea.setColumns(20);
        reasonTextArea.setFont(new java.awt.Font("Monospaced", 0, 12));
        reasonTextArea.setRows(5);
        jScrollPane2.setViewportView(reasonTextArea);

        cancelButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("Work slate:");

        assignedDateTextField.setFont(new java.awt.Font("Tahoma", 0, 14));
        assignedDateTextField.setText("N/A");
        assignedDateTextField.setEnabled(false);

        workslatesComboBox.setFont(new java.awt.Font("Tahoma", 0, 14));
        workslatesComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<none>" }));
        workslatesComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                workslatesComboBoxItemStateChanged(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setText("Assigned date:");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spring1.png"))); // NOI18N
        jButton1.setToolTipText("Click to select work presets.");

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/winter1.png"))); // NOI18N
        jButton2.setToolTipText("Click to select work presets.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(workslatesComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(assignedDateTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(43, 43, 43)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(routineButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(urgentButton))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE))))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(239, Short.MAX_VALUE)
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chargeToTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(workslatesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(assignedDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(chargeToTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(routineButton)
                    .addComponent(urgentButton))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        order.setReason(this.reasonTextArea.getText());
        order.setDescr(this.workRequiredTextArea.getText());

        if(this.urgentButton.isSelected())
            order.setPriority(GroundsRequest.Priority.URGENT);
        
        else if(this.routineButton.isSelected())
            order.setPriority(GroundsRequest.Priority.ROUTINE);

        order.setChargeTo(this.chargeToTextField.getText());

        if(this.workslatesComboBox.getSelectedIndex() != 0) {
            assigned = (WorkSlate) this.workslatesComboBox.getSelectedItem();

            WorkDate date = Helper.toDate(this.assignedDateTextField.getText());

//            if(date == null) {
//                JOptionPane.showMessageDialog(this, "Invalid assigned date format",
//                        "Grounds", JOptionPane.ERROR_MESSAGE);
//                return;
//            }

            order.setAssignedDate(date);
        }
        else {
            assigned = null;
            order.setAssignedDate(null);
        }

        dispose();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void workslatesComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_workslatesComboBoxItemStateChanged
        if(this.workslatesComboBox.getSelectedIndex() == 0) {
            this.assignedDateTextField.setText("N/A");
            return;
        }
        
        GroundsDate date = new GroundsDate(System.currentTimeMillis());
        
        this.assignedDateTextField.setText(date.toString());
    }//GEN-LAST:event_workslatesComboBoxItemStateChanged

    public WorkSlate getWorkSlate() {
        return assigned;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField assignedDateTextField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField chargeToTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.ButtonGroup priorityButtonGroup;
    private javax.swing.JTextArea reasonTextArea;
    private javax.swing.JRadioButton routineButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JRadioButton urgentButton;
    private javax.swing.JTextArea workRequiredTextArea;
    private javax.swing.JComboBox workslatesComboBox;
    // End of variables declaration//GEN-END:variables

}
